<%@ include file="/en/jsp/static.jsp" %>
<%@ page import="linuxbox.poll.logic.*,linuxbox.admin.logic.*" %>
<%@ taglib uri="/linuxbox/common.tld" prefix="common" %>

<jsp:include page="/en/jsp/header.jsp" flush="true"/>
<jsp:useBean id="home" class="linuxbox.poll.logic.BoxPoll"/>

<%
  String VIEW_TYPE = (String) session.getAttribute("poll.VIEW_TYPE");
  if (VIEW_TYPE==null)
  {
    VIEW_TYPE = sec.getConfigString("poll", "General", "Default View");
    session.setAttribute("poll.VIEW_TYPE", VIEW_TYPE);
  }
  String SORTBY = (String) session.getAttribute("poll.SORTBY");
  if (SORTBY==null)
  {
    SORTBY = "createtime";
    session.setAttribute("poll.SORTBY", SORTBY);
  }

  boolean sortAsc = true;
  String tmp = (String) session.getAttribute("poll.ASC");
  if (tmp!=null && tmp.length() > 0)
    sortAsc = new Boolean(tmp).booleanValue();
  else
    session.setAttribute("poll.ASC", "true");

  int curPage = 0;
  try
  {
    String PAGE = (String) request.getParameter("PAGE");
    if (PAGE != null)
      curPage = Integer.parseInt(PAGE);
  }
  catch (Exception e)
  {
  }

  int entriesPerPage = sec.getConfigInt("poll", "General", "Entries Per Page");
  int totalPage = home.getTotalPages(entriesPerPage, VIEW_TYPE);
  if (curPage >= totalPage)
    curPage = totalPage-1;

  java.util.Iterator it = home.getPolls(VIEW_TYPE, SORTBY, sortAsc, entriesPerPage*curPage, entriesPerPage);
  int count = 0;
  String pollJSP = BoxStatic.jspDir+"poll/BoxPoll.jsp";
  String pollAction = BoxStatic.servletDir+"BoxPollAction";
  UserManager um = UserManager.getInstance();
%>

<script Language="JavaScript">
  function confirmDel(entry)
  {
    return confirm("Are you sure to delete " + entry);
  }
  function onChangeViewType()
  {
    document.theForm.submit();
  }
</script>

<FORM action="<%=BoxStatic.servletDir%>BoxPollAction" method="POST" name="theForm" >

<table width="100%" height=26 align=middle border=0 cellpadding=0 cellspacing=0 bgcolor="#f4f4f4" style="font-size:16px;color:#d05000">
  <tr>
    <td>
      &nbsp;<img src="<%=BoxStatic.imageDir%>itemball.jpg" width=14 height=15 border=0 align="absmiddle">
      <b><%=boxName%> - Poll</b>
    </td>
    <td align=right>
      <a href="<%=BoxStatic.servletDir%>BoxPollSettingsAction?SubmitEdit=edit">
        Settings
      </a>
    </td>
  </tr>
</table>


<hr width="100%" color=#E06000 align=left>

<table width="100%" border=0 cellpadding=0 cellspacing=0>
<tr><td>
  <table width="100%">
    <tr>
      <td>
        <input type="submit" name="SubmitEdit" value="Create a new poll">
      </td>
      <td>View
          <select name="VIEW_TYPE" size="1" onchange="onChangeViewType()">
            <option <% if (VIEW_TYPE.compareTo(home.VT_ALL) == 0) out.print(" selected ");%> value="<%=home.VT_ALL%>">All</option>
            <option <% if (VIEW_TYPE.compareTo(home.VT_OPEN) == 0) out.print(" selected ");%> value="<%=home.VT_OPEN%>">Open polls</option>
            <option <% if (VIEW_TYPE.compareTo(home.VT_CLOSED) == 0) out.print(" selected ");%> value="<%=home.VT_CLOSED%>">Closed polls</option>
          </select>
      </td>
      <td align=right>
        <common:Page curPage="<%=curPage%>" totalPage="<%=totalPage%>" action="<%=pollJSP%>" />
      </td>
    </tr>
  </table>
</td></tr>

<tr><td>
<%

  if ( it != null && it.hasNext())
  {
%>
  <TABLE cellSpacing=1 cellPadding=0 width="100%" border=0 bgColor=#dddddd>
    <TR bgColor=#7EC1F3>
    <%
      if (VIEW_TYPE.compareTo(home.VT_OPEN) == 0)  // For the open polls
      {
        String[] headers = {"Poll", "Create Time", "Polling Ends", "Votes", "Create By", "Edit", "Conclude", "Delete"};
        String[] sortby = {"subject", "createtime", "concludetime", "", "createdby", "", "", ""};
    %>
      <common:ListHeader headers="<%=headers%>" sortby="<%=sortby%>" curSortBy="<%=SORTBY%>" sortAsc="<%=sortAsc%>" action="<%=pollAction%>" />
    </TR>

    <common:Iteration name="entry" type="linuxbox.poll.logic.BoxPollEntry" group="<%=it%>">
      <tr <% if ((count++ % 2) == 0) out.print("bgcolor=#f0f0f0"); %>>
        <TD>
          <a href="<%=BoxStatic.servletDir%>BoxPollVoteAction?SubmitView=view&ID=<%=entry.getID()%>"><%=entry.getSubject()%></a>
        </TD>
        <TD>
          <%=entry.getCreateTimeString()%>
        </TD>
        <TD>
          <%=entry.getConcludeTimeString()%>
        </TD>
        <TD align=center>
          <%=home.getTotalReplies(entry.getID())%>
        </TD>
        <TD>
          <%=um.getUserDisplayName(entry.getCreatedBy(), um.USERNAME_FORMAT_ALL)%>
        </TD>
  <%  if (sec.isPollAdmin() || curID == entry.getCreatedBy() )
      {
  %>
        <TD align=center>
          <a href="<%=BoxStatic.servletDir%>BoxPollAction?SubmitEdit=edit&ID=<%=entry.getID()%>">Edit</a>
        </TD>
        <TD align=center>
          <a href="<%=BoxStatic.servletDir%>BoxPollConcludeAction?SubmitView=view&ID=<%=entry.getID()%>">Conclude</a>
        </TD>
        <TD align=center>
          <a href="<%=BoxStatic.servletDir%>BoxPollAction?SubmitDelete=delete&ID=<%=entry.getID()%>"
          Onclick='return confirmDel("<%=entry.getSubject()%>")'
          >Delete</a>
        </TD>
  <%  }
      else
      {
        out.print("<TD align=center></TD><TD align=center></TD><TD align=center></TD>");
      }
  %>
      </TR>
    </common:Iteration>

<%    }
      else if (VIEW_TYPE.compareTo(home.VT_CLOSED) == 0)   // For the closed polls
      {
        String[] headers = {"Poll", "Create Time", "Polling Ends", "Votes", "Create By", "Delete"};
        String[] sortby = {"subject", "createtime", "concludetime", "", "createdby", ""};
      %>
      <common:ListHeader headers="<%=headers%>" sortby="<%=sortby%>" curSortBy="<%=SORTBY%>" sortAsc="<%=!sortAsc%>" action="<%=pollAction%>" />
    </TR>

    <common:Iteration name="entry" type="linuxbox.poll.logic.BoxPollEntry" group="<%=it%>">
      <tr <% if ((count++ % 2) == 0) out.print("bgcolor=#f0f0f0"); %>>
        <TD>
          <a href="<%=BoxStatic.servletDir%>BoxPollVoteAction?SubmitView=view&ID=<%=entry.getID()%>"><%=entry.getSubject()%></a>
        </TD>
        <TD>
          <%=entry.getCreateTimeString()%>
        </TD>
        <TD>
          <%=entry.getConcludeTimeString()%>
        </TD>
        <TD align=center>
          <%=home.getTotalReplies(entry.getID())%>
        </TD>
        <TD>
          <%=um.getUserDisplayName(entry.getCreatedBy(), um.USERNAME_FORMAT_ALL)%>
        </TD>
  <%  if (sec.isPollAdmin() || curID == entry.getCreatedBy())
      {
  %>
        <TD align=center>
          <a href="<%=BoxStatic.servletDir%>BoxPollAction?SubmitDelete=delete&ID=<%=entry.getID()%>"
          Onclick='return confirmDel("<%=entry.getSubject()%>")'
          >Delete</a>
        </TD>
  <%  }
      else
      {
        out.print("<TD align=center></TD>");
      }
  %>

      </TR>
    </common:Iteration>

<%    }
      else   // For all the polls
      {
        String[] headers = {"Poll", "Create Time", "Polling Ends", "Votes", "Status", "Create By", "Edit", "Conclude", "Delete"};
        String[] sortby = {"subject", "createtime", "concludetime", "", "closed", "createdby", "", "", ""};
      %>
      <common:ListHeader headers="<%=headers%>" sortby="<%=sortby%>" curSortBy="<%=SORTBY%>" sortAsc="<%=!sortAsc%>" action="<%=pollAction%>" />
    </TR>

    <common:Iteration name="entry" type="linuxbox.poll.logic.BoxPollEntry" group="<%=it%>">
      <tr <% if ((count++ % 2) == 0) out.print("bgcolor=#f0f0f0"); %>>
        <TD>
          <a href="<%=BoxStatic.servletDir%>BoxPollVoteAction?SubmitView=view&ID=<%=entry.getID()%>"><%=entry.getSubject()%></a>
        </TD>
        <TD>
          <%=entry.getCreateTimeString()%>
        </TD>
        <TD>
          <%=entry.getConcludeTimeString()%>
        </TD>
        <TD align=center>
          <%=home.getTotalReplies(entry.getID())%>
        </TD>
        <TD>
      <%
          if (entry.getClosed())
            out.print("Closed");
          else
            out.print("Open");
      %>
        </TD>
        <TD>
          <%=um.getUserDisplayName(entry.getCreatedBy(), um.USERNAME_FORMAT_ALL)%>
        </TD>
  <%  if (sec.isPollAdmin() || curID == entry.getCreatedBy())
      {
        if (entry.getClosed())
        {
          out.print("<TD align=center></TD><TD align=center></TD>");
        }
        else
        {
  %>
        <TD align=center>
          <a href="<%=BoxStatic.servletDir%>BoxPollAction?SubmitEdit=edit&ID=<%=entry.getID()%>">Edit</a>
        </TD>
        <TD align=center>
          <a href="<%=BoxStatic.servletDir%>BoxPollConcludeAction?SubmitView=view&ID=<%=entry.getID()%>">Conclude</a>
        </TD>
  <%
        }
  %>
        <TD align=center>
          <a href="<%=BoxStatic.servletDir%>BoxPollAction?SubmitDelete=delete&ID=<%=entry.getID()%>"
          Onclick='return confirmDel("<%=entry.getSubject()%>")'
          >Delete</a>
        </TD>
  <%  }
      else
      {
        out.print("<TD align=center></TD><TD align=center></TD><TD align=center></TD>");
      }
  %>
      </TR>
    </common:Iteration>
<%
    }
%>

  </TABLE>
</td></tr>

<tr><td>
  <table width="100%">
    <tr>
      <td>
        <input type="submit" name="SubmitEdit" value="Create a new poll">
      </td>
      <td>View
          <select name="VIEW_TYPE" size="1" onchange="onChangeViewType()">
            <option <% if (VIEW_TYPE.compareTo(home.VT_ALL) == 0) out.print(" selected ");%> value="<%=home.VT_ALL%>">All</option>
            <option <% if (VIEW_TYPE.compareTo(home.VT_OPEN) == 0) out.print(" selected ");%> value="<%=home.VT_OPEN%>">Open polls</option>
            <option <% if (VIEW_TYPE.compareTo(home.VT_CLOSED) == 0) out.print(" selected ");%> value="<%=home.VT_CLOSED%>">Closed polls</option>
          </select>
      </td>
      <td align=right>
        <common:Page curPage="<%=curPage%>" totalPage="<%=totalPage%>" action="<%=pollJSP%>" />
      </td>
    </tr>
  </table>
</td></tr>

<%
  }
  else
  {
%>
    <TABLE cellSpacing=1 cellPadding=1 width="100%" border=0 bgColor=#dddddd>
      <TR>
        <TD align=center bgColor=#eeeeee>There are no polls.</TD>
      </TR>

    </TABLE>
</td></tr>
<%
  }
%>


</table>
<hr width=\"100%\" color=#E06000 align=left>

</FORM>

<jsp:include page="/en/jsp/footer.jsp" flush="true"/>
